
import 'package:baby/Entities/Comment/Comment.dart';
import 'package:baby/Entities/Record/PhotoRecord.dart';
import 'package:baby/Entities/Record/RecordTag.dart';
import 'package:baby/Entities/Record/RecordType.dart';
import 'package:baby/Entities/Record/TextRecord.dart';
import 'package:baby/Entities/Record/VideoRecord.dart';
import 'package:baby/UI/Pages/HomePage/RecordList.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_icons/font_awesome.dart';
import 'package:intl/intl.dart' show DateFormat;

class MomentsPage extends StatefulWidget {
  @override State createState() => _MomentsPageState();
}

class _MomentsPageState extends State<MomentsPage> with SingleTickerProviderStateMixin, AutomaticKeepAliveClientMixin {
  TabController controller;
  var currentPage = 0.0;
  var items = <TextRecord>[];

  @override void initState(){
    controller = TabController(vsync: this, length: 2);
    super.initState();


    controller.animation.addListener((){
      setState(() {
        currentPage = controller.animation.value;
      });
    });

    void addItems() {
      items.add(PhotoRecord(
        tag: RecordTag(
            icon: "https://banner2.kisspng.com/20180509/zde/kisspng-avatar-youtube-8-ball-pool-user-5af328dc1be026.0480517715258851481142.jpg",
            label: "دۇنياغا كۆز ئاچتىڭ"
        ),
        content: "ئۇ تۇغۇلدى",
        type: RecordType.Photo,
        photos: [
          "https://www.bloomingbath.com/media/wysiwyg/Downloads/IMG_1690.jpg",
          "https://images-na.ssl-images-amazon.com/images/G/01/aplusautomation/vendorimages/94ad8a08-62e4-46f1-84a4-a7c1481c6564.jpg._CB294152044_.jpg",
          "https://beachfrontbabywraps.com/wp-content/uploads/2016/03/pbsling-Cropped.jpg",
          "https://sep.yimg.com/ay/albee-baby/baby-k-tan-baby-carrier-in-natural-organic-extra-large-40.jpg"
        ],
      ));

      var comment0 = Comment(
        content: "ۋاي، بەك يىغلايدىكەنلا.😼",
        dateTime: DateTime.now(),
        likeCount: 10,
        unlikeCount: 3,
      );
      var comment1 = Comment(
          content: "ھە شۇندا",
          dateTime: DateTime.now(),
          likeCount: 10,
          unlikeCount: 3,
          replayTo: comment0
      );
      var comment2 = Comment(
          content: "شۇندامىتە.😼",
          dateTime: DateTime.now(),
          likeCount: 10,
          unlikeCount: 3,
          replayTo: comment1
      );
      var comment3 = Comment(
        content: "بۇ سۆزلەپ كەلسەك گەپ جىق ، بۇ بۇندىن ئىككى ئاي بۇرۇن بولغان ۋەقە . ئەڭ ئاۋۋال توربەتنىڭ ئۈندىدار كىچىك پىروگراممىسىدىن چاتاق چىقتى . يەنى api نىڭ https دىن چاتاق چىقتى. ۋاقتى ئۆتەپ كىتىپتىمىشلەر كىيىن ئۇقسام .شۇنىڭ بىلەن بىرە كۈن ئىيتىشىپ مۇلازىمىتىېرنى ئاندا مۇندا قىلىپ ئىشقىلىپ مالماتاڭ قىلىپ ئېغىلدەكلا قىپ قويدۇم",
        dateTime: DateTime.now(),
        likeCount: 0,
        unlikeCount: 10,
      );
      var comment4 = Comment(
          content: "كاللىسىدىن كەتكەن بالىكەن ما ئۈستىدىكىسى",
          dateTime: DateTime.now(),
          likeCount: 10,
          unlikeCount: 3,
          replayTo: comment3
      );

      items.add(PhotoRecord(
          content: "يىغلا بالام، تازا يىغلا، تەڭ يىغلايلى...😭 ئۇھ...",
          type: RecordType.Photo,
          photos: [
            "https://www.boldsky.com/img/2014/07/15-mancrying.jpg",
            "http://illumination.missouri.edu/s13/images/nn/cry_baby.jpg",
            "https://assets.parents.com/s3fs-public/styles/nfp_1080_portrait/public/ZdorovKirillVladimirovich_0.jpg?HZo9_mI3Bdq5KNVEKWPF9SPETsqtRISw",
            "http://www.evolutionaryparenting.com/wp-content/uploads/2011/08/crying-baby.jpg",
            "http://www.slate.com/content/dam/slate/blogs/behold/2013/08/Jill%20Greenberg/8.jpg.CROP.original-original.jpg"
          ],
          comments: [
            comment0,
            comment1,
            comment2,
            comment3,
            comment4
          ]
      ));

      items.add(TextRecord(
          type: RecordType.Text,
          content: "شۇنىڭ بىلەن ساڭا بىر ئىسىم قويىلى دەپ بالام، مەن قويىمەن دىگەننى ئاپاڭ دىگەن خوتۇن قويغىلى قويمىدى..."
      ));

      items.add(VideoRecord(
          content: "ئاپاڭ دىگەن ساراڭ خوتۇن بىلەن ئۇرۇشۇپ قالدۇق بالام.",
          type: RecordType.Photo,
          videos: [
            "https://carwad.net/sites/default/files/pictures-of-husband-and-wife-fighting-150433-1976937.jpg",
            "https://www.premierboxingchampions.com/sites/default/files/Vargas-vs-Herrera.jpg",
            "http://media.salemwebnetwork.com/cms/CW/Couples/33985-fight-couplefighting-marriage.1200w.tn.jpg",
            "https://cn.bing.com/th?id=OIP.ottiQm0wCZLdDEAGGcDM-AAAAA&pid=Api&rs=1&p=0",
          ]
      ));

      items.add(TextRecord(
          type: RecordType.Text,
          content: "كومپىيۇتېر ئىلمىنىڭ ئىككىنچى چوڭ تارمىقى كومپىيۇتېر قۇرۇلۇشى. كومپىيۇتېرنى لايىھەلەش تولىمۇ چوڭ جەڭنىڭ بىرى، چۈنكى ئاستىنقى قاتلامدىكى قاتتىق دېتالدىن ئۈستىنكى قاتلامدىكى يۇمشاق دېتالغىچە نۇرغۇن ئوخشىمايدىغان تەرەپلەرنى ئويلىشىشقا توغرا كېلىدۇ. لايىھەلىگۈچى چوقۇم كومپىيۇتېرنى ئىمكانقەدەر ئەلالاشتۇرۇشتۇرۇپ قۇربىنىڭ يېتىشىچە تېخىمۇ كۆپ مەسىلەرنى ھەل قىلىشقا كاپالەتلىك قىلىشى كېرەك"
      ));
    }
    addItems();
    addItems();
    addItems();
  }

  @override Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
            child: Column(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(1.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      _buildTabButton(context: context, tabIndex: 0, location: _TabButtonLocation.start, text: "ھەممىسى", onPressed: (){
                        controller.animateTo(0);
                      }),
                      _buildTabButton(context: context, tabIndex: 1, location: _TabButtonLocation.end, text: "ئەگەشكەنلىرىم", onPressed: (){
                        controller.animateTo(1);
                      }),
                    ],
                  ),
                ),
                Expanded(
                    child: TabBarView(
                      controller: controller,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.all(5.0),
                          child: ListView(
                              children: [
                                RecordList(records: items,)
                              ]
                          ),
                        ),
                        Container(color: Colors.black.withOpacity(0.1), margin: EdgeInsets.all(5.0),),
                      ],
                    )
                )
              ],
            )
        )
    );
  }

  Widget _buildTabButton({@required BuildContext context, @required int tabIndex, @required _TabButtonLocation location, @required String text, @required void onPressed()}){
    var primaryColor = Theme.of(context).primaryColor;
    var selectedColor = Color.lerp(primaryColor, Colors.white, currentPage);
    var selectedReverseColor = Color.lerp(Colors.white, primaryColor, currentPage);
    var dir = Directionality.of(context);
    var isStart = true;
    if (location == _TabButtonLocation.start && dir == TextDirection.rtl)
      isStart = true;
    if (location == _TabButtonLocation.start && dir == TextDirection.ltr)
      isStart = false;
    if (location == _TabButtonLocation.end && dir == TextDirection.rtl)
      isStart = false;
    if (location == _TabButtonLocation.end && dir == TextDirection.ltr) {
      isStart = true;
    }

    //isStart = location == _TabButtonLocation.end && Directionality.of(context) == TextDirection.rtl;

    var borderRadius = isStart ? BorderRadius.only(
        topRight: Radius.circular(15.0),
        bottomRight: Radius.circular(15.0)
    ) : BorderRadius.only(
        topLeft: Radius.circular(15.0),
        bottomLeft: Radius.circular(15.0)
    );

    return Container(
      child: FlatButton(
        color: location == _TabButtonLocation.start ? selectedColor : selectedReverseColor,
        child: Text(text, style: TextStyle(color: location == _TabButtonLocation.start ? selectedReverseColor : selectedColor),),
        onPressed: onPressed,
        shape: RoundedRectangleBorder(
            borderRadius: borderRadius,
            side: BorderSide(
                color: primaryColor,
                width: 1.0,
                style: BorderStyle.solid
            )
        ),
      ),
      constraints: BoxConstraints(
          minWidth: 120.0
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}

enum _TabButtonLocation {
  start,
//  center,
  end,
}
